<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

	function __construct()
	{
		parent:: __construct();
	}
	
##################################################################################################################################
//check username and password

	function veryfy_user($username, $password)
	{
		
		$params = array('iterationCount'=>'8', 'portableHashes'=>'false');
		$this->load->library('PasswordHash', $params);
		//$enc_password = $this->passwordhash->HashPassword($password);
		

		/*
		$q = $this
				->db
				->where('username',$username)
				->where('password',$enc_password)
				->limit(1)
				->get('useraccounts');
		*/
		
		
		$query = "SELECT * FROM useraccounts where username=?";
		
		$q = $this->db->query($query,$username);
		
		

		if ($q->num_rows >0)
		{
			
			//check password 
			
			$ok = 0;
			
			$row =  $q->row();
			 
			$enc_password_check = $this->passwordhash->CheckPassword($password, $row->password);
			
			if ($enc_password_check == 1)
			{
				
				return $q;

			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	
	}

########################################################################################################
	//check is excisting user
	function is_registered_user($username)
	{
		$query = "SELECT * FROM useraccounts WHERE username = ?";
		$q = $this->db->query($query,$username);
	
	
		if ($q->num_rows() == 0)
		{
				
			return false;
		}
		else
		{
			return true;
		}
	
	
	}
	
	##########################################################################################################
	//insert data into database
	
	function user_register($fname, $lname, $email, $username, $password, $accessLevel)
	{
		
	$data = array(
			'firstName' => $fname,
			'lastName' => $lname,
			'email' => $email,
			'username' => $username,
			'password' => $password,
			'accessLevel' => $accessLevel
	);
	
			$this->db->insert('useraccounts', $data);
	
			return true;
	}
}
?>